package com.example.demo.result;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import javax.print.DocFlavor;
import java.io.Serializable;

/**
 * @author zhanpengguo
 * @date 2019-07-03 17:38
 */
@Data
public class JsonResult implements Serializable {

    public static final String SUCCESS = "0";
    public static final String FAIL = "1";

    @Getter
    @Setter
    String code = SUCCESS;

    @Getter
    @Setter
    String message = "success";

    @Getter
    @Setter
    Object data;

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

    public JsonResult(Object data){
        this.data = data;
    }

    public JsonResult(Object data, String message){
        this.message = message;
        this.data = data;
    }

    public JsonResult(String code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public JsonResult(Throwable throwable){
        this.code = FAIL;
        this.message = throwable.getMessage();
    }

    public static JsonResult success(String message, Object data){
        return new JsonResult("200", message, data);
    }

}
